const mysql = require('./../utils/selfDB')
// 0查看收藏列表
function queryCollectingList(ctx) {
  var openid = ctx.state.$wxInfo.userinfo.openId; //'用户号' , 
  var functype = ctx.request.body.functype;  // '功能类型 SYSTEM.MENU' , 
  var search = ctx.request.body.search;//查询条件
  var beginseq = ctx.request.body.beginseq * 1;//开始条件
  var querynum = ctx.request.body.querynum * 1;//查询条数

  var rawsql = ' 1';
  var varset = {};

  if (search) {
    rawsql = ' (act.title like :search or act.indexing like :search)'
    varset.search = '%' + search + '%'
  }
  if (beginseq) {
    varset.begin = beginseq
    varset.querynum = querynum
    rawsql = rawsql + ' and  uci.orders < :begin order by uci.orders desc limit :querynum'//{ search: '%' + search + '%' ,
  } else {
    rawsql = rawsql + ' order by uci.orders desc limit :querynum';
    varset.querynum = querynum
  }
  return mysql.select('act.*').from('user_collecting_info as uci').innerJoin('author_create_title as act', function () {
    this.on('uci.author', '=', 'act.openid').andOn('uci.seqid', '=', 'act.seqid').andOn('uci.functype', '=', 'act.functype')
  })
    .where({ 'uci.openid': openid, 'uci.functype': functype }).andWhereRaw(rawsql, varset)
    .then(data => {
      return {
        status: '0',
        msg: data
      }
    })
    .catch(e => {
      return {
        status: '-1',
        msg: "query collecting Article List error:" + e.message
      }
    })
}

//1新增收藏 
function addCollecting(ctx) {
  var openid = ctx.state.$wxInfo.userinfo.openId; //'用户号' , 
  var orders = 0;//ctx.request.body.orders;
  var functype = ctx.request.body.functype;
  var author = ctx.request.body.author;
  var seqid = ctx.request.body.seqid;  // '内容顺序号' , 
  //1 查询最大序号 2新增数据
  return mysql.max('orders as orders').from('user_collecting_info').where({
    openid
  }).then(data => {
    if (data[0].orders && data[0].orders != null) {
      orders = data[0].orders * 1 + 1;
    } else {
      orders = 1;
    }
    //插入collecting表
    return mysql().insert({
      openid, orders, functype, author, seqid
    }).into('user_collecting_info')
      .then(function () {
        return {
          status: '0',
          msg: '收藏成功'
        }
      })
      .catch(e => {
        return {
          status: '-1',
          msg: "collecting error:" + e.message
        }
      })
  })
    .then(msg => {
      return msg
    })
    .catch(e => {
      return {
        status: '-1',
        msg: "select orders error:" + e.message
      }
    })

}
//2取消收藏 
function delCollecting(ctx) {
  var openid = ctx.state.$wxInfo.userinfo.openId; //'用户号' , 
  var author = ctx.request.body.author;
  var seqid = ctx.request.body.seqid;  // '内容顺序号' , 

  //删除原数据
  return mysql.del().from('user_collecting_info').where({ openid, author, seqid })
    .then(function () {
      return {
        status: '0',
        msg: "取消收藏成功"
      }
    })
    .catch(e => {
      return {
        status: '-1',
        msg: "update detail error:" + e.message
      }
    })
}
//3根据内容判断当前用户是否收藏
function whetherCollected(ctx) {
  var openid = ctx.state.$wxInfo.userinfo.openId; //'用户号' , 
  var author = ctx.request.body.author;
  var seqid = ctx.request.body.seqid;  // '内容顺序号' , 

  return mysql.count('orders as orders').from('user_collecting_info').where({
    openid, author, seqid
  }).then(data => {
    if (data[0].orders) {
      return {
        status: '0',
        msg: true
      }
    } else {
      return {
        status: '0',
        msg: false
      }
    }
  })
    .catch(e => {
      return {
        status: '-1',
        msg: "select whetherCollecting error:" + e.message
      }
    })
}

module.exports = (ctx, next) => {
  var userinfo = ctx.state.$wxInfo.userinfo;
  try {
    var opflag = ctx.request.body.opflag;//0查看收藏列表 1新增收藏 2取消收藏 3根据内容判断当前用户是否收藏
    if (opflag == '0') {
      return queryCollectingList(ctx).then(result => {
        ctx.state.data = result
      })
    } else if (opflag == '1') {
      return addCollecting(ctx).then(result => {
        ctx.state.data = result;
      })
    } else if (opflag == '2') {
      return delCollecting(ctx).then(result => {
        //result.userinfo = userinfo;
        ctx.state.data = result;
      })
    } else if (opflag == '3') {//更新
      return whetherCollected(ctx).then(result => {
        ctx.state.data = result
      })
    }
  } finally {

  }
}